/**
 * 
 */
package com.qhgrain.api.comm.datadictionary.provider;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.Response;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.pubframework.common.vo.RestVo;
import com.qhgrain.api.comm.IDataDicRestProvider;
import com.qhgrain.api.comm.datadictionary.entity.DataDictionary;
import com.qhgrain.api.comm.datadictionary.service.IDataDictionaryService;

@Service
public class DataDicRestProvider implements IDataDicRestProvider {
	@Autowired
	private IDataDictionaryService dataDictionaryService;

	@Override
	public Response queryById(String dicId) {
		RestVo restVo = new RestVo();
		try {
			DataDictionary dataDictionary = dataDictionaryService
					.getDataDicById(dicId);
			restVo.setSuccess(dataDictionary);
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}

	@Override
	public Response add(String dicId, String dcCode, String dcName,
			String dcPcode, String dcSysCode,
			String dcIsDeleted, String dcCreateTime, String dcLastUpdateTime,
			String dcRemark, String dcCreateUser, String dcSysName) {
		RestVo restVo = RestVo.getInstance();
		try {
			DataDictionary dataDictionary = new DataDictionary();
			dataDictionary.setDcCode(dcCode);
			//默认当前时间
			if(StringUtil.isEmpty(dcCreateTime)){
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				dcCreateTime = df.format(new Date());
			}
			dataDictionary.setDcCreateTime(dcCreateTime);
			dataDictionary.setDcCreateUser(dcCreateUser);
			dataDictionary.setDcId(dicId);
			//默认未删除
			dcIsDeleted = StringUtil.isEmpty(dcIsDeleted) ? "0" : dcIsDeleted;
			dataDictionary.setDcIsDeleted(dcIsDeleted);
			//默认新增时间
			if(StringUtil.isEmpty(dcLastUpdateTime)){
				dcLastUpdateTime = dcCreateTime;
			}
			dataDictionary.setDcLastUpdateTime(dcLastUpdateTime);
			dataDictionary.setDcName(dcName);
			dataDictionary.setDcPcode(dcPcode);
			dataDictionary.setDcRemark(dcRemark);
			dataDictionary.setDcSysCode(dcSysCode);
			dataDictionary.setDcSysName(dcSysName);
			;
			dataDictionaryService.insertDic(dataDictionary);
			restVo.setSuccess(null);
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}

	@Override
	public Response delete(String dicId) {
		RestVo restVo = RestVo.getInstance();
		try {
			if(StringUtil.isNotEmpty(dicId)){
				String[] ids = dicId.split(",");
				for(String id :ids){
					dataDictionaryService.deleteById(id);
				}
			}
			restVo.setSuccess(null);
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}

	@Override
	public Response query(String pageNo, String pageSize,
			String conditionJsonStr) {
		RestVo restVo = RestVo.getInstance();

		try {
			JSONObject jsonObjcet = JSONObject.fromObject(conditionJsonStr);
			Map<String, String> conditions = null;
			if (StringUtil.isNotEmpty(conditionJsonStr)) {
				conditions = new HashMap<String, String>();
				Set<String> set = jsonObjcet.keySet();
				for (String key : set) {
					conditions.put(key, jsonObjcet.getString(key));
				}
			}

			Page<DataDictionary> vo = dataDictionaryService.queryListForPage(
					Integer.parseInt(pageNo, 10),
					Integer.parseInt(pageSize, 10), conditions);
			Page<DataDictionary> vo2 = dataDictionaryService.queryListForPage(
					1, 9999999, conditions);
			restVo.setSuccess(vo);
			restVo.setExceptionMsg(String.valueOf(vo2.getTotal()));
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}

	/**
	 * 
	 * 2016年4月9日 下午8:37:51
	 * 
	 * @tags 添加字典所属应用名
	 * @author zhaoyq
	 */
	@Override
	public Response update(String dicId, String dcCode, String dcName,
			String dcPcode, String dcSysCode,
			String dcIsDeleted, String dcCreateTime, String dcLastUpdateTime,
			String dcRemark, String dcCreateUser, String dcSysName) {
		RestVo restVo = RestVo.getInstance();
		try {
			DataDictionary dataDictionary = new DataDictionary();
			dataDictionary.setDcCode(dcCode);
			dataDictionary.setDcCreateTime(dcCreateTime);
			dataDictionary.setDcCreateUser(dcCreateUser);
			dataDictionary.setDcId(dicId);
			dataDictionary.setDcIsDeleted(dcIsDeleted);
			dataDictionary.setDcLastUpdateTime(dcLastUpdateTime);
			dataDictionary.setDcName(dcName);
			dataDictionary.setDcPcode(dcPcode);
			dataDictionary.setDcRemark(dcRemark);
			dataDictionary.setDcSysCode(dcSysCode);
			dataDictionary.setDcSysName(dcSysName);
			;
			dataDictionaryService.update(dataDictionary);
			restVo.setSuccess(null);
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}

	/**
	 * 根据系统编码和父节点得到所有下级字典树
	 */
	@Override
	public Response getDataDicTree(String sysCode, String dicPcode) {
		RestVo restVo = RestVo.getInstance();
		try {
			List<DataDictionary> dataDictionaries = dataDictionaryService
					.getChildDataDic(sysCode, dicPcode);
			String rs = net.sf.json.JSONArray.fromObject(dataDictionaries)
					.toString();
			restVo.setSuccess(rs);
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}

	/**
	 * 根据系统编码和父节点得到直接下级字典
	 */
	@Override
	public Response getChildDataDic(String sysCode, String dicPcode) {
		RestVo restVo = RestVo.getInstance();
		try {
			Map<String, String> conditionMap = new HashMap<String, String>();
			if(StringUtil.isNotEmpty(dicPcode)&&!"null".equals(dicPcode)){
				conditionMap.put("dcPcode", dicPcode);
			}
			if(StringUtil.isNotEmpty(sysCode) &&!"null".equals(sysCode)){
				conditionMap.put("dcSysCode", sysCode);
			}
			//默认查启用的
			conditionMap.put("dcIsDeleted", "0");
			List<DataDictionary> dataDictionaries = dataDictionaryService
					.queryListForPage(1, Integer.MAX_VALUE, conditionMap);
			PageInfo<DataDictionary> pageInfo = new PageInfo<DataDictionary>(
					dataDictionaries);
			restVo.setSuccess(net.sf.json.JSONObject.fromObject(pageInfo)
					.toString());
		} catch (Exception e) {
			restVo.setFail(e.getMessage());
		}
		return Response.ok(restVo).build();
	}
	@Override
	public Response updateStatusByIds(String ids, String status) {
		RestVo vo = new RestVo();
		try {
			List<String> list =Arrays.asList(ids.split(","));
			this.dataDictionaryService.updateStaus(list, status);
			vo.setSuccess("");
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}
}
